# -*- encoding: utf8 -*-
from django.shortcuts import render_to_response, get_object_or_404
from MediaManager.models import Media
from django.template import RequestContext
from MediaManager.forms import FormMedia

def listing(request):
    item_list = Media.objects.all()
    return render_to_response('list.html', {'item_list':item_list})

def add (request):
    if request.method == 'POST':
        form = FormMedia(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return render_to_response('save.html', {})     
    else:
        form = FormMedia()
    return render_to_response('add.html', {'form':form},
                              context_instance=RequestContext(request))

def media(request, number_media):
    media = get_object_or_404(Media, pk=number_media)
    if request.method == 'POST':
        form = FormMedia(request.POST, request.FILES, instance=media)
        if form.is_valid():
            form.save()
            return render_to_response('save.html', {})
    else:
        form = FormMedia(instance=media)                 
    return render_to_response('media.html', {'form':form},
                                  context_instance=RequestContext(request))
